package com.tobie.schedule.controller;


import com.common.uaa.entity.UserEntity;
import com.common.utils.ResultVo;
import com.tobie.schedule.pojo.entity.ScheduleEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Tobieance
 * @description
 * @date 2023-11-20 10:50
 */
@RestController
@Slf4j
public class ScheduleController {

    /**
     * <h1>整型字段</h1>
     *用于存储要获取到期日程的用户id
     */
    private Integer alertUserId;

    /**
     *用于存储到期预定
     */
    private List<ScheduleEntity> latestScheduleEntitys;


    /**
     * 用于给定时任务调用，获取用户id
     *
     * @return {@link Integer}
     */
    public Integer getAlertUserId() {
        return alertUserId;
    }


    /**
     * 向前端发送最新的到期日程信息
     *
     * @param request http请求
     * @return {@link ResultVo}<{@link List}<{@link ScheduleEntity}>>
     */
    @GetMapping("/alert")
    public ResultVo<List<ScheduleEntity>> getLatestScheduleVos(HttpServletRequest request) {
        log.info("进入页面控制器getLatestScheduleVos方法\t");
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        UserEntity userEntity = (UserEntity) authentication.getPrincipal();
        log.info("username:\t"+userEntity.getUsername());
        alertUserId=userEntity.getUserId();
        log.info("user id:\t"+alertUserId.toString());
        List<ScheduleEntity> messages = new ArrayList<>(latestScheduleEntitys);
        latestScheduleEntitys.clear(); // 获取到最新的消息后清空缓存
        return ResultVo.success(messages);
    }


    /**
     * @param scheduleEntities 所有
     */
    public void sendScheduleEntity(List<ScheduleEntity> scheduleEntities){
        this.latestScheduleEntitys=scheduleEntities;
    }

}
